﻿using Microsoft.AspNetCore.Mvc.ApplicationModels;
using My.Core.DynamicWebApi.Attributes;
using Panda.DynamicWebApi;
using System.Reflection;

namespace My.Core.DynamicWebApi
{
	internal class ServiceActionRouteFactory : IActionRouteFactory
	{
		public string CreateActionRouteModel(string areaName, string controllerName, ActionModel action)
		{
			var controllerType = action.ActionMethod.DeclaringType;
			var serviceAttribute = controllerType.GetCustomAttribute<ServiceAttribute>();

			var _controllerName = serviceAttribute.ServiceName == string.Empty ? controllerName.Replace("Service", "") : serviceAttribute.ServiceName.Replace("Service", "");

			return $"api/{_controllerName}/{action.ActionName.Replace("Async", "")}";
		}
	}
}
